package com.webdesign.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webdesign.dto.HomeworkInfoDTO;
import com.webdesign.dto.MyPage;
import com.webdesign.entity.TbTerm;
import com.webdesign.service.ITbHomeworkService;
import com.webdesign.service.ITbTermService;

import freemarker.template.TemplateException;

@Controller("TermSelectAction")
@Scope("prototype")
public class TermSelectAction extends ActionSupport{
	private static final String REDIRECT = "redirect";
	private ITbTermService iTbTermService;
	private ITbHomeworkService iTbHomeworkService;
	private String terminfo;
	private String numpage;
	private MyPage page;
	private List<TbTerm> terms;
	private List<HomeworkInfoDTO> homeworks;
	
	/*
	 * 教师登录以后，首先是返回所有学年列表，让用户先选择学年
	 */
	public String showTerms() throws IOException, TemplateException{
		terms = iTbTermService.showTerms();
		return SUCCESS;
	}
	
	/*
	 * 选择学年以后，在主页返回所选学年的所有作业以及学年的下拉列表
	 */
	public String selectTerm(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		String userid = (String)session.get("userid");
		homeworks =  iTbHomeworkService.showHomework(terminfo,userid);
		ActionContext.getContext().getSession().put("termid", terminfo);
		
		String pageNumber = CookieAction.getCookie("pagenumber");
				
		if(page == null){
			page = new MyPage();
			page.setNumber(1);
		}
		if(pageNumber != null && pageNumber!=""){
			page.setNumber(Integer.parseInt(pageNumber));
		}
		
		page.setTotalElements(homeworks.size());
		homeworks = homeworks.subList((int)(page.getStart()), (int)(page.getEnd()));
		
		terms = iTbTermService.showTerms();
		String termname = iTbTermService.getTermName(terminfo);
		ActionContext.getContext().getSession().put("termname", termname);
		
		return SUCCESS;
	}

	public ITbTermService getiTbTermService() {
		return iTbTermService;
	}
	
	@Resource(name="TbTermService")
	public void setiTbTermService(ITbTermService iTbTermService) {
		this.iTbTermService = iTbTermService;
	}

	public ITbHomeworkService getiTbHomeworkService() {
		return iTbHomeworkService;
	}

	@Resource(name="TbHomeworkService")
	public void setiTbHomeworkService(ITbHomeworkService iTbHomeworkService) {
		this.iTbHomeworkService = iTbHomeworkService;
	}

	public String getTerminfo() {
		return terminfo;
	}

	public void setTerminfo(String terminfo) {
		this.terminfo = terminfo;
	}
	

	public String getNumpage() {
		return numpage;
	}

	public void setNumpage(String numpage) {
		this.numpage = numpage;
	}

	public MyPage getPage() {
		return page;
	}

	public void setPage(MyPage page) {
		this.page = page;
	}

	public List<TbTerm> getTerms() {
		return terms;
	}

	public void setTerms(List<TbTerm> terms) {
		this.terms = terms;
	}

	public List<HomeworkInfoDTO> getHomeworks() {
		return homeworks;
	}

	public void setHomeworks(List<HomeworkInfoDTO> homeworks) {
		this.homeworks = homeworks;
	}
	
	
	
}
